package com.dykj.fireFighting.web.dutymanger;

import java.util.*;

import com.dykj.fireFighting.api.socialUnit.dutymanger.dto.PersonnelManagementDto;
import com.dykj.fireFighting.api.socialUnit.dutymanger.entity.PersonnelManagement;
import com.dykj.fireFighting.api.socialUnit.dutymanger.service.IPersonnelManagementService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;



/**
 * 值班管理-人员管理接口
 *
 * @Author aixiaolan
 * @Date  2021-06-22 11:09:31
 */
@Slf4j
@Api(value = "值班管理-人员管理接口", tags = {"值班管理-人员管理接口"})
@RestController
@RequestMapping("dutymanger/personnelManagement")
public class PersonnelManagementController {
   @DubboReference
    private IPersonnelManagementService personnelManagementService;

    /**
     * 获取单位id
     * @return String
     */
    public String getEnterpriseId() {
        try {
            return Long.toString(ShiroUtil.getUser().getEnterpriseId());
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 获取用户名称
     * @return String
     */
    public String getName() {
        try {
            return ShiroUtil.getUser().getName();
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 值班管理-人员管理列表
     *
     * @return
     */
    @AutoLog(value = "值班管理-人员管理-分页列表查询")
    @ApiOperation(value = "值班管理-人员管理分页列表", notes = "值班管理-人员管理分页列表")
    @GetMapping("/list")
  //  @RequiresPermissions("dutymanger/personnelManagement/list")
    public CommonResult<PageVo<PersonnelManagement>> treeGrid(PersonnelManagementDto commonDto) {
        if (StringUtils.isBlank(commonDto.getUnitId())) {
            if (StringUtils.isBlank(getEnterpriseId())) {
                throw new BaseException("请登录系统");
            }
            commonDto.setUnitId(getEnterpriseId());
        }
        PageVo<PersonnelManagement> page = personnelManagementService.pageList(commonDto);
        return CommonResult.success(page);
    }

    /**
     * 获取值班管理-人员管理信息
     */
    @AutoLog(value = "值班管理-人员管理-获取值班管理-人员管理信息")
    @ApiOperation(value = "获取值班管理-人员管理信息", notes = "获取值班管理-人员管理信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
   // @RequiresPermissions("dutymanger/personnelManagement/info")
    public CommonResult<PersonnelManagement> info(@PathVariable("id") String id){
        PersonnelManagement personnelManagement = personnelManagementService.getById(id);
        return CommonResult.success(personnelManagement);
    }

    /**
     * 保存值班管理-人员管理信息
     */
    @AutoLog(value = "值班管理-人员管理-保存值班管理-人员管理信息")
    @ApiOperation(value = "保存值班管理-人员管理信息", notes = "保存值班管理-人员管理信息")
    @PostMapping("/save")
   // @RequiresPermissions("dutymanger/personnelManagement/save")
    public CommonResult save(@Valid @RequestBody PersonnelManagement personnelManagement) {
        try {
            personnelManagement.setCreateTime(new Date());
            personnelManagement.setUpdateTime(new Date());
            if (StringUtils.isBlank(getEnterpriseId())) {
                throw new BaseException("请登录系统");
            }else {
                personnelManagement.setUnitId(getEnterpriseId());
                personnelManagement.setCreateBy(getName());
            }
            personnelManagementService.save(personnelManagement);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException("运行异常，请联系管理员");
        }
    }

    /**
     * 修改值班管理-人员管理信息
     */
    @AutoLog(value = "值班管理-人员管理-修改值班管理-人员管理信息")
    @ApiOperation(value = "修改值班管理-人员管理信息", notes = "修改值班管理-人员管理信息")
    @PostMapping("/update")
   // @RequiresPermissions("dutymanger/personnelManagement/update")
    public CommonResult update(@Valid @RequestBody PersonnelManagement personnelManagement) {
        try {
            personnelManagement.setUpdateTime(new Date());
            personnelManagement.setUpdateBy(getName());
                personnelManagementService.updateById(personnelManagement);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException("运行异常，请联系管理员");
        }
    }

    /**
     * 删除值班管理-人员管理信息
     */
    @AutoLog(value = "值班管理-人员管理-删除值班管理-人员管理信息")
    @ApiOperation(value = "删除值班管理-人员管理信息", notes = "删除值班管理-人员管理信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
   // @RequiresPermissions("dutymanger/personnelManagement/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                personnelManagementService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException("运行异常，请联系管理员");
        }
    }
}
